package com.gree.auth.config.service.impl;

import com.gree.auth.config.service.DefaultUserDetailsService;
import com.gree.auth.service.SysUserExService;
import com.gree.auth.service.SysUserSaasService;
import com.gree.framework.entity.CusUserDetails;
import com.gree.framework.entity.SaasTenant;
import com.gree.framework.entity.SysUserEx;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by 180551 on 2019/6/15.
 * 安全用户认证实现类(主要表为：scm_sys_user)
 */
@Service
@Primary
public class DafaultUserDetailsServiceImpl implements DefaultUserDetailsService {

    private static final BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();

    @Autowired
    private SysUserExService sysUserExService;

    @Autowired
    private SysUserSaasService sysUserSaasService;

    @Override
    public CusUserDetails loadUserByUsername(String userinfo) throws UsernameNotFoundException {

        String client_id = "external_client";
        HttpServletRequest httpServletRequest = getHttpServletRequest();
        if(httpServletRequest != null){
            client_id = httpServletRequest.getParameter("client_id").toString();
        }

        if(client_id.equals("external_client")){
            SysUserEx user = sysUserExService.findByUserName(userinfo);
            if(user == null){
                throw new UsernameNotFoundException("用户不存在!");
            }

            CusUserDetails userDetails = CusUserDetails.withUsername(user.getUserName())
                    .password(user.getPwd())
                    .authorities(getAuthorities())
                    .build();

            return userDetails;
        }else{
            SaasTenant user = sysUserSaasService.findByUserName(userinfo);
            if(user == null){
                throw new UsernameNotFoundException("用户不存在!");
            }

            CusUserDetails userDetails = CusUserDetails.withUsername(user.getSysUid())
                    .password(user.getPassword())
                    .authorities(getAuthorities())
                    .build();

            return userDetails;
        }
    }

    private HttpServletRequest getHttpServletRequest() {
        try {
            return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public Collection<? extends GrantedAuthority> getAuthorities() {
        Collection<GrantedAuthority> collection = new HashSet<>();
        return collection;
    }

}
